using UnityEngine;

public class PlayerPrimaryAttackState : PlayerState
{
    public int comboCounter { get; private set; } = 0;
    private float lastTimeAttacked;
    private float comboWindow = 1.2f;

    public PlayerPrimaryAttackState(Player player, string animBoolName) : base(player, animBoolName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        // AudioManager.Instance.PlaySfx(2);
        xInput = 0; // we need this to fix bug on attack direction
        if (comboCounter > 2 || lastTimeAttacked + comboWindow < Time.time)
            comboCounter = 0;
        player.Anim.SetInteger("comboCounter", comboCounter);
        // choose attack direction
        int arrackDirection = player.FacingDirection;
        if (xInput != 0)
        {
            arrackDirection = xInput;
        }
        //player.Anim.speed = 1.3f;
        player.SetVelocity(player.attackMovement[comboCounter].x * arrackDirection, player.attackMovement[comboCounter].y);
        stateTimer = 0.1f;
    }

    public override void Exit()
    {
        base.Exit();

        _ = player.StartCoroutine("BusyFor", 0.1f);
        //player.Anim.speed = 1;

        comboCounter++;
        lastTimeAttacked = Time.time;
    }

    public override void Update()
    {
        base.Update();
        if (stateTimer < 0)
            player.SetVelocityToZero();
        if (triggerCalled)
            StateMachine.ChangeState(player.IdleState);
    }
}